package com.heima.media.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.dto.PageResponseResult;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.enums.MediaEnum;
import com.heima.media.dto.ChannelDto;
import com.heima.media.entity.WmChannel;
import com.heima.media.entity.WmNews;
import com.heima.media.mapper.WmChannelMapper;
import com.heima.media.service.IWmChannelService;
import com.heima.media.service.IWmNewsService;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * @author: itheima
 * @create: 2022-07-26 12:26
 */
@Service
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements IWmChannelService {
    @Autowired
    private IWmNewsService wmNewsService;

    /**
     * 频道条件分页查询
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult listByName(ChannelDto dto) {
        //1.构建分页对象
        IPage<WmChannel> page = new Page<>(dto.getPage(), dto.getSize());
        //2.构建查询条件
        LambdaQueryWrapper<WmChannel> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(dto.getName())) {
            queryWrapper.like(WmChannel::getName, dto.getName());
        }
        //3.执行查询
        this.page(page, queryWrapper);

        //4.封装响应结果
        //int i = 1 / 0;
        return new PageResponseResult(dto.getPage(), dto.getSize(), page.getTotal(), page.getRecords());
    }

    /**
     * 保存频道
     *
     * @param entity
     * @return
     */
    @Override
    public ResponseResult saveChannel(WmChannel entity) {
        //1.根据名称查询频道是否存在
        LambdaQueryWrapper<WmChannel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WmChannel::getName, entity.getName());
        int count = this.count(queryWrapper);

        //2.不存在执行新增
        if (count == 0) {
            entity.setIsDefault(false);
            entity.setStatus(false);
            entity.setOrd(1);
            this.save(entity);
            return ResponseResult.okResult();
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
    }

    /**
     * 修改频道
     *
     * @param entity
     * @return
     */
    @Override
    public ResponseResult updateChannel(WmChannel entity) {
        if (ObjectUtils.isEmpty(entity)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        LambdaUpdateWrapper<WmChannel> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(WmChannel::getId, entity.getId());
        if (entity.getName() != null) {
            wrapper.set(WmChannel::getName, entity.getName());
        }
        if (entity.getDescription() != null) {
            wrapper.set(WmChannel::getDescription, entity.getDescription());
        }
        if (entity.getStatus() != null) {
            wrapper.set(WmChannel::getStatus, entity.getStatus());
        }
        if (entity.getOrd() != null) {
            wrapper.set(WmChannel::getOrd, entity.getOrd());
        }
        boolean update = this.update(wrapper);
        if (update) {
            return ResponseResult.okResult();
        } else {
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }
    }

    /**
     * 删除频道
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteChannel(Integer id) {
        System.err.println("========" + id);
        LambdaQueryWrapper<WmNews> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WmNews::getChannelId, id);
        Map<String, Object> map = wmNewsService.getMap(wrapper);
        if (map == null) {
            boolean b = this.removeById(id);
            if (b) {
                return ResponseResult.okResult(id);
            } else {
                return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
            }
        } else {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_CAN_NOT_DELETE);
        }
    }
}
